<!doctype html>
<html>
<head>
    <include file="public:headtop"/>
    <link href="/static/css/general.css" rel="stylesheet">
    <link href="/static/css/result.css" rel="stylesheet">
    <link href="/static/css/address.css" rel="stylesheet">
    <link href="/static/css/jquery_dialog.css" rel="stylesheet">
    <link href="/static/css/animate.css" rel="stylesheet">
    <script src="/static/js/jquery_dialog.js"></script>
</head>

<body>
<include file="public:headmini1"/>
<ul class="result-box margin-bottom-10">
    <li>
        <div class="oh">
            <div class="result-img pull-left">
                <img src="{$item.img}" alt=""/>
            </div>
            <div class="result-text pull-left">
                <div class="result-h1">{$item.title}</div>
                <div class="result-h2">适用范围：<span>{$item.syfw}</span></div>
                <div class="result-h2">
                    透光率：<span class="margin-right-5">{$item.touguangdu}%</span>
                    防晒指数：<span class="margin-right-5">{$item.fangshai}%</span>
                    隔热率：<span class="margin-right-5">{$item.geredu}%</span>
                </div>
            </div>
        </div>
        <div class="result-h3 oh">
            <div class="pull-left text-blue margin-right-10">价格：<span>￥{:number_format($item['price'], 2)}元</span></div>
            <!-- <div class="pull-left text-red">现价：<span>￥<?php echo $item['price']*0.8;?></span></div> -->
            <!-- <div class="pull-left text-blue margin-right-10">单价：<span>￥{:number_format($item['price'], 2)}元</span></div> -->
            <!-- <div class="pull-left text-red">奖励：<span>￥{:number_format($item['jianglimax'], 2)}元起</span></div> -->
            <!--<div class="btn-box pull-right"><button>立即购买</button></div>-->
        </div>
    </li>
</ul>

<form class="dz-box">
    <ul>
        <li class="li01">
            <span class="tx">填写收货地址</span>
            <!--<div class="dz" onclick="JqueryDialog.Open('历史收货记录', 'index.php?m=Item&a=address',300, 300);">-->
            <div class="dz">
                <span>历史收货地址</span>

                <div class="img-box"><img src="/static/images/img28.png" alt="icon"></div>
            </div>
        </li>
        <li class="li02">
            <span>收件人</span>
            <input type="text" placeholder="输入框" id="in01" value="{$lst_addr.user}">
        </li>
        <li class="li02">
            <span>联系方式</span>
            <input type="text" placeholder="输入框" id="in02" value="{$lst_addr.tel}">
        </li>
        <li class="li03">
            <span>收件地址</span>
            <textarea type="text" id="in03" placeholder="输入框">{$lst_addr.addr}</textarea>
        </li>
        <!--<li class="li02">
            <span>可用优惠</span>
            <select id="sy_num_gid2">
                <option value="0">----</option>
                <volist name="ojl" id="vo">
                    <option value="{$vo.id}">{$vo.count}</option>
                </volist>
            </select>
        </li>-->
        <li class="li04">
            <span class="shul">购买数量</span>

            <p class="p_num">
                <span class="sy_minus" id="sy_minus_gid1">-</span>
                <input class="sy_num" id="sy_num_gid1" readonly type="text" name="number1" value="1"/>
                <span class="sy_plus" id="sy_plus_gid1">+</span>
            </p>

            <p class="pull-left">{$good_type}</p>
        </li>
        <li class="li02" style="width:100%">
            <span style="width:31.5%">优惠码</span>
            <input type="text"  style="width:58.5%" id="discount_code" value="">
        </li>
    </ul>
    <div class="last-box">
        <p class="text-center">总计：<span id="Jiage">0</span>元</p>
        <button id="Credit">立即支付</button>
        <!--<button surl="{:U('User/index')}" data="{:U('Item/jiesuan', array('id'=>$item[id]))}">立即支付</button>-->
    </div>
    <input type='hidden' class='myprice' value='{$item.price}'/>
    <input type='hidden' class='num' value='{$o_num}'/>
    <input type='hidden' class='goods' value='{$item.id}'/>
    <input type='hidden' class='f' value='{$f}'/>
</form>

<div class="f-box" style="display:none;">
    <div class="box">
        <div class="bt"><span>历史地址记录</span></div>
        <volist name="addr_list" id="vo">
            <p class="addr_history">
                <font class='p1'>{$vo.address}</font>
                <font class='p2'>{$vo.user_name}</font>
                (<font class='p3'>{$vo.user_tel}</font>)
            </p>
        </volist>
    </div>
</div>
<script src="/static/assets/curency.js"></script>
<script type="text/javascript">
    var _validata_discount_code = false;//优惠卷是否正确
    var _discount = 1;//折扣
    var price = 0;
    var _price = $('.myprice').val();//原价
    document.body.addEventListener('touchstart', function () {
    });

    function initNumberJiajian() {
        price = $('.myprice').val() * _discount;//商品单价
        var num = parseInt($('.num').val(), 10);
        price = price <= 0 ? 0 : price;
        var sy_number = num ? num : 1;//默认购买数量
        var maxNumber = 0;//商品购买上限，0不限制

        var sy_num_gid1 = $('#sy_num_gid1');
        var jiageEle = $('#Jiage');

        function setNumber(num) {
            sy_number = num;
            sy_num_gid1.val(sy_number);
            var number = sy_number * price;
            jiageEle.html(number.formatMoney(2, '￥'));
            $('.myprice').val(number);
        }

        setNumber(sy_number);

        $('#sy_minus_gid1').click(function () {
            sy_number > 1 && setNumber(sy_number - 1);
        });

        $('#sy_plus_gid1').click(function () {
            if (maxNumber == 0 || sy_number < maxNumber) {
                setNumber(sy_number + 1);
            }
        });

         //验证优惠码
        $('#discount_code').blur(function(){
            var discount_code = $('#discount_code').val();
            if (discount_code) {
                $.getJSON("{:U('Item/validate_discount_code')}",{'discount_code':discount_code},function(data){
                    if (data.code===-1) {
                        alert(data.message)
                    }else{
                        // alert(data.message)
                        _discount = data.message;
                        _validata_discount_code = true;
                        price = _price * _discount;
                        setNumber(sy_number);
                    }
                })
            }else{
                _discount = 1;
                _validata_discount_code = false;
                price = _price * _discount;
                setNumber(sy_number);
            }
        });
    }

    var n = 0;
    function anima() {
        if (n < 1) {
            n = n + 1
        } else {
            n = 0
        }
        ;
        if (n == 1) {
            $('.f-box').css('display', 'block').addClass('animated fadeIn');
        }
        if (n == 0) {
            $('.f-box').css('display', 'none');
        }
    }

    $(function () {
        
        //初始化数字增减
        initNumberJiajian();
        //初始化对话框
        //TODO
        $('.dz').click(function () {
            anima();
        });
        $('.f-box').click(function () {
            anima();
        });

        $('.addr_history').click(function () {
            var p1 = $(this).find('.p1');
            var p2 = $(this).find('.p2');
            var p3 = $(this).find('.p3');
            $('#in01').val(p2.html());
            $('#in02').val(p3.html());
            $('#in03').text(p1.text());
        });

        var modal = new Modal({
            ele: 'Modal',
            confirm: function() {
                modal.hide();
                saveOrder();
            }
        });

        //显示用户协议
        $('form').submit(function(e) {
            e.preventDefault();
            modal.show();
        });
    });

    function saveOrder() {
        var DATA = {}
        var addr_user = $("#in01").val();
        DATA.addr_user = addr_user;
        var addr_tel = $("#in02").val();
        DATA.addr_tel = addr_tel;
        var addr_addr = $("#in03").val();
        DATA.addr_addr = addr_addr;
        var item_num = $('.sy_num').val();
        DATA.item_num = item_num;
       
        var item_price = $('.myprice').val();
        item_price = item_price*1;
        DATA.item_price = item_price.toFixed(2);

        var item_id = $('.goods').val();
        DATA.item_id = item_id;
        var f = $('.f').val();
        DATA.f = f;
        var ojl = $('#sy_num_gid2').val();
        DATA.ojl = ojl;
        var url = $(this).attr('data');
        if (_validata_discount_code) {
            DATA.discount_code = $('#discount_code').val();
        }
        //var success_url = $(this).attr('surl');

        $.post(url, DATA, function (ref) {
            ref = eval("(" + ref + ")");
            if (ref.status) {
//                location.href = '/wpay/js_api_call.php';
                jiesuan(ref['sn']);
            } else {
                alert(ref.message);
            }
        });
        return false;
    }

//    $('.last-box>button').click();

    function jiesuan(sn) {

        $.getJSON("{:U('pay')}", function(data) {
            // alert(JSON.stringify(data))

            WeixinJSBridge.invoke('getBrandWCPayRequest', data.data,
                function(res){
                    WeixinJSBridge.log(res.err_msg);
                    var ref_msg = res.err_msg;
                    if(ref_msg == 'get_brand_wcpay_request:ok'){
                        location.href='/index.php?m=Item&a=update_order&sn=' + sn;
                    }else if(ref_msg == 'get_brand_wcpat_request:cancel'){
                        alert('支付已经取消！');
                    } else {
                        alert('支付失败！');
                    }
                }
            );
        })
    }

</script>

<!-- 用户协议模态框 -->
<div class="modal" id="Modal">
    <div class="modal-dialog">
        <div class="modal-body">
            <h3 class="text-center">用户协议</h3>

<!-- 
            <p>我们为易安友汽车光学隔热膜提供五年质保承诺

我们始终坚持为客户提供高品质产品与服务，并不断优化产品特性与服务细节。
E&U的产品均符合严苛的品控标准与认证；
E&U的技师需通过专业的实操培训与测试；
E&U的施工要按照规范的操作流程与进度；
E&U的产品保证无裂纹、无破裂、不剥落、不起皮或从玻璃表面脱落。
</p> -->
            <p>我们为易安友汽车光学隔热膜提供五年质保承诺</p>
            <p>我们始终坚持为客户提供高品质产品与服务，并不断优化产品特性与服务细节。</p>
            <p>E&U的产品均符合严苛的品控标准与认证；</p>
            <p>E&U的技师需通过专业的实操培训与测试；</p>
            <p>E&U的施工要按照规范的操作流程与进度；</p>
            <p>E&U的产品保证无裂纹、无破裂、不剥落、不起皮或从玻璃表面脱落。</p>

         <!--    <p>Lorem ipsum dolor sit.</p>
            <p>Lorem ipsum dolor sit.</p>
            <p>Lorem ipsum dolor sit.</p>
            <p>Lorem ipsum dolor sit.</p> -->
        </div>
        <div class="modal-footer">
            <button class="btn" data-toggle="confirm">同意</button>
            <button class="btn" data-toggle="cancel">不同意</button>
        </div>
    </div>
</div>

</body>
</html>